package com.shanghaionstar.mro.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.onstar.cn.common.log.LogManager;
import com.shanghaionstar.R;

import java.util.ArrayList;

public class FenbeiWave extends View {
	private Bitmap voice_line;
	private static final String TAG = "FenbeiWave";
	private ArrayList<Float> volumes=new ArrayList<Float>();
	private float scale = 0.03f;
	private int x;
	private boolean isFirstRecord = false;
	private Paint paint;
	private int height;
	private float centerY;

	public ArrayList<Float> getVolumes() {
		return volumes;
	}

	public void setVolumes(ArrayList<Float> volumes) {
		this.volumes = volumes;
	}

	public FenbeiWave(Context context) {
		super(context);
		init();
	}

	private void init() {
		paint = new Paint();
		isFirstRecord = true;
	}

	public FenbeiWave(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public boolean isFirstRecord() {
		return isFirstRecord;
	}

	public void setFirstRecord(boolean isFirstRecord) {
		this.isFirstRecord = isFirstRecord;
	}

	private float getRandomAdjust() {
		float adjustNum = (float) ((Math.random() * 10 + 1) / 10);
		return adjustNum;
	}

	private float getDPSize(int px) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, getResources().getDisplayMetrics());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		// invalidate();
		super.onDraw(canvas);
		height = getHeight();
		if (centerY == 0 && height != 0) {
			centerY = (height - getDPSize(100)) / 2 - getDPSize(40);
		}
		if (!isFirstRecord) {
			LogManager.getInstance().d("valumns ---->>"+volumes);
			if (volumes != null && volumes.size() != 0) {
				ArrayList<Float> tmpVolumes = (ArrayList<Float>) volumes.clone();
				volumes.clear();
				for (int i = 0; i < tmpVolumes.size(); i++) {
					scale = tmpVolumes.get(i) <= 0 ? 1 : tmpVolumes.get(i);
					if (scale > 40 && scale <= 50) {
						scale = scale / 600f;
					} else if (scale > 50 && scale <= 60) {
						scale = scale / 700f;
					} else if (scale > 60 && scale <= 70) {
						scale = scale / 500f;
					} else if (scale > 70 && scale <= 80) {
						scale = scale / 300f;
					} else {
						scale = scale / 300f;
					}
					for (; x < 60; x++) {
						// bitmap = getZoom(0.01f);
						Bitmap bitmap = getZoom(scale + getRandomAdjust());
						canvas.drawBitmap(bitmap, x * 27, centerY - bitmap.getHeight() / 2, paint);
						recycle(canvas, bitmap);
					}
				}
			}else {
				//when no voice, display the voice line
				for (; x < 60; x++) {
					Bitmap bitmap = getZoom(0.06f);
					canvas.drawBitmap(bitmap, x * 27, centerY - bitmap.getHeight() / 2, paint);
					recycle(canvas, bitmap);
				}
			}
		} else {
			for (; x < 60; x++) {
				recycle(canvas, null);
				isFirstRecord = false;
			}
		}
		if (x > 59) {
			x = 0;
		}
	}

	private void recycle(Canvas canvas, Bitmap bitmap) {
//		if (canvas != null) {
//			canvas.save();
//			canvas.restore();
//		}
//		 if (voice_line != null) {
//			 voice_line.recycle();
//			 voice_line = null;
//		 }
		if (bitmap != null) {
			bitmap.recycle();
			//bitmap = null;
		}
	}

	private Bitmap tempBitmap;

	public Bitmap getZoom(float scale) {
		Matrix matrix = new Matrix();
		matrix.postScale(1f, scale * 2);
		if (voice_line == null) {
			voice_line = BitmapFactory.decodeResource(getResources(), R.drawable.mro_voice_line_short_noshadow);
		}
		int width = voice_line.getWidth();
		int height = voice_line.getHeight();
		Bitmap bitmap = Bitmap.createBitmap(voice_line, 0, 0, width, height, matrix, true);
		// if (tempBitmap == null) {
		// tempBitmap = bitmap;
		// }
		// if (bitmap.sameAs(tempBitmap)) {
		//
		// }
		return bitmap;
	}

	// public Bitmap getShortZoom() {
	// Matrix matrix = new Matrix();
	// if (voice_line_short == null) {
	// voice_line_short = BitmapFactory.decodeResource(getResources(),
	// R.drawable.voice_line_short_noshadow);
	// }
	// int width = voice_line_short.getWidth();
	// int height = voice_line_short.getHeight();
	// Bitmap bitmap = Bitmap.createBitmap(voice_line_short, 0, 0, width,
	// height, matrix, true);
	//
	// return bitmap;
	// }
}
